<template>
  <div class="register-container">
    <div class="register-card">
      <div class="register-header">
        <h2>用户注册</h2>
        <p>创建您的账户，开始使用溯源管理系统</p>
      </div>
      
      <el-form
        ref="registerForm"
        :model="formData"
        :rules="rules"
        label-width="100px"
        class="register-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="formData.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            clearable
          />
        </el-form-item>
        
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="formData.email"
            placeholder="请输入邮箱地址"
            prefix-icon="Message"
            clearable
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="formData.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>
        
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="formData.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>
        
        <el-form-item label="真实姓名" prop="realName">
          <el-input
            v-model="formData.realName"
            placeholder="请输入真实姓名"
            prefix-icon="UserFilled"
            clearable
          />
        </el-form-item>
        
        <el-form-item label="手机号码" prop="phone">
          <el-input
            v-model="formData.phone"
            placeholder="请输入手机号码"
            prefix-icon="Phone"
            clearable
          />
        </el-form-item>
        
        <el-form-item label="用户角色" prop="role">
          <el-select
            v-model="formData.role"
            placeholder="请选择用户角色"
            style="width: 100%"
          >
            <el-option label="超市管理员" value="admin" />
            <el-option label="采购员" value="purchaser" />
            <el-option label="质检员" value="inspector" />
            <el-option label="普通用户" value="user" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="所属部门" prop="department">
          <el-input
            v-model="formData.department"
            placeholder="请输入所属部门"
            prefix-icon="OfficeBuilding"
            clearable
          />
        </el-form-item>
        
        <el-form-item>
          <el-checkbox v-model="formData.agreeTerms">
            我已阅读并同意
            <el-link type="primary" @click="showTerms">《用户协议》</el-link>
            和
            <el-link type="primary" @click="showPrivacy">《隐私政策》</el-link>
          </el-checkbox>
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="handleRegister"
            style="width: 100%"
          >
            立即注册
          </el-button>
        </el-form-item>
        
        <div class="login-link">
          已有账户？
          <el-link type="primary" @click="goToLogin">立即登录</el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { defineComponent, ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

export default defineComponent({
  name: 'Register',
  setup() {
    const router = useRouter()
    const registerForm = ref(null)
    const loading = ref(false)
    
    const formData = reactive({
      username: '',
      email: '',
      password: '',
      confirmPassword: '',
      realName: '',
      phone: '',
      role: '',
      department: '',
      agreeTerms: false
    })
    
    // 表单验证规则
    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' },
        { pattern: /^[a-zA-Z0-9_]+$/, message: '用户名只能包含字母、数字和下划线', trigger: 'blur' }
      ],
      email: [
        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' },
        { pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/, message: '密码必须包含大小写字母和数字', trigger: 'blur' }
      ],
      confirmPassword: [
        { required: true, message: '请确认密码', trigger: 'blur' },
        {
          validator: (rule, value, callback) => {
            if (value !== formData.password) {
              callback(new Error('两次输入密码不一致'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }
      ],
      realName: [
        { required: true, message: '请输入真实姓名', trigger: 'blur' },
        { min: 2, max: 10, message: '姓名长度在 2 到 10 个字符', trigger: 'blur' }
      ],
      phone: [
        { required: true, message: '请输入手机号码', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
      ],
      role: [
        { required: true, message: '请选择用户角色', trigger: 'change' }
      ],
      department: [
        { required: true, message: '请输入所属部门', trigger: 'blur' }
      ]
    }
    
    // 注册处理
    const handleRegister = async () => {
      if (!formData.agreeTerms) {
        ElMessage.warning('请先同意用户协议和隐私政策')
        return
      }
      
      try {
        await registerForm.value.validate()
        loading.value = true
        
        // 模拟注册请求
        await new Promise(resolve => setTimeout(resolve, 2000))
        
        ElMessage.success('注册成功！请登录')
        router.push('/login')
      } catch (error) {
        console.error('注册失败:', error)
        ElMessage.error('注册失败，请检查输入信息')
      } finally {
        loading.value = false
      }
    }
    
    // 跳转到登录页
    const goToLogin = () => {
      router.push('/login')
    }
    
    // 显示用户协议
    const showTerms = () => {
      ElMessageBox.alert(
        '这里是用户协议内容...',
        '用户协议',
        {
          confirmButtonText: '确定',
          callback: () => {}
        }
      )
    }
    
    // 显示隐私政策
    const showPrivacy = () => {
      ElMessageBox.alert(
        '这里是隐私政策内容...',
        '隐私政策',
        {
          confirmButtonText: '确定',
          callback: () => {}
        }
      )
    }
    
    return {
      registerForm,
      formData,
      rules,
      loading,
      handleRegister,
      goToLogin,
      showTerms,
      showPrivacy
    }
  }
})
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.register-card {
  width: 100%;
  max-width: 500px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 40px;
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.register-header h2 {
  color: #2563eb;
  margin-bottom: 10px;
  font-size: 28px;
}

.register-header p {
  color: #666;
  font-size: 16px;
}

.register-form {
  margin-top: 20px;
}

.login-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
}

@media (max-width: 768px) {
  .register-card {
    padding: 20px;
  }
  
  .register-header h2 {
    font-size: 24px;
  }
}
</style> 